System.register(["@angular/http", "ng2-layer", "@angular/core", "@angular/router", "@angular/forms"], function (exports_1, context_1) {
    "use strict";
    var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
        var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
        if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
        else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
        return c > 3 && r && Object.defineProperty(target, key, r), r;
    };
    var __metadata = (this && this.__metadata) || function (k, v) {
        if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
    };
    var __moduleName = context_1 && context_1.id;
    var http_1, ng2_layer_1, core_1, router_1, forms_1, BoxCategoryComp, AddCategoryComp, EditCategoryComp, _a, _b, _c;
    return {
        setters: [
            function (http_1_1) {
                http_1 = http_1_1;
            },
            function (ng2_layer_1_1) {
                ng2_layer_1 = ng2_layer_1_1;
            },
            function (core_1_1) {
                core_1 = core_1_1;
            },
            function (router_1_1) {
                router_1 = router_1_1;
            },
            function (forms_1_1) {
                forms_1 = forms_1_1;
            }
        ],
        execute: function () {
            BoxCategoryComp = (function () {
                function BoxCategoryComp(http, router, layer, parent) {
                    var _this = this;
                    this.http = http;
                    this.layer = layer;
                    this.parent = parent;
                    this.categories = [];
                    this.products = [];
                    dragDropSort({
                        axis: "Y",
                        sortable: true,
                        dragItems: ".category_item:not(.category_header)",
                        sortContainer: ".categories",
                        drop: function (a, item) {
                            a.classList.remove("hover");
                            if (a != item.parentNode) {
                                a.appendChild(item);
                            }
                        }
                    });
                    router.parent.params.subscribe(function (params) {
                        _this.boxId = params.id;
                        http.get({
                            search: {
                                m: "Home",
                                c: "ApiPublic",
                                a: "getGoodsTypeByBoxId",
                                box_id: _this.boxId
                            }
                        }).then(function (data) {
                            _this.categories = data.data;
                        });
                        _this.loadProduct();
                    });
                }
                BoxCategoryComp.prototype.deleteCategory = function (ev, cate) {
                    var _this = this;
                    ev.stopPropagation();
                    var ly = this.layer.confirm({
                        message: "确定要删除该分类么?"
                    }).ok(function () {
                        _this.http.get({
                            search: {
                                m: "Admin",
                                c: "ApiGoodsType",
                                a: "deleteType",
                                type_id: cate.type_id
                            }
                        }).then(function () {
                            _this.categories = _this.categories.filter(function (c) {
                                return !(c.type_id == cate.type_id);
                            });
                            ly.close();
                        });
                    }).cancel(function () {
                        ly.close();
                    });
                };
                BoxCategoryComp.prototype.loadProduct = function (cate) {
                    var _this = this;
                    this.cateId = cate ? cate.type_id : null;
                    this.http.get({
                        search: {
                            m: "Admin",
                            c: "ApiGoods",
                            a: "getGoodsList",
                            box_id: this.boxId,
                            type: this.cateId
                        }
                    }).then(function (data) {
                        _this.products = data.data.goods_data;
                    });
                };
                BoxCategoryComp.prototype.addCategory = function () {
                    this.layer.dialog({
                        title: "添加分类",
                        dialogComponent: AddCategoryComp,
                        parent: this.parent,
                        data: { parent: this },
                        imports: [forms_1.FormsModule]
                    });
                };
                BoxCategoryComp.prototype.selectType = function (good, cate, e) {
                    if (!e.target.checked) {
                        good.typeInfo = good.typeInfo.filter(function (i) {
                            return i.type_id != cate.type_id;
                        });
                    }
                    else {
                        good.typeInfo.push(cate);
                    }
                    this.updateGoodCategory(good);
                };
                BoxCategoryComp.prototype.updateGoodCategory = function (good) {
                    this.http.post({
                        search: {
                            m: "Admin",
                            c: "ApiGoods",
                            a: "updateGoods",
                            goods_id: good.goods_id,
                            box_id: this.boxId
                        },
                        body: {
                            types: JSON.stringify(good.typeInfo.map(function (t) {
                                return t.type_id;
                            }))
                        }
                    }).then(function (data) {
                    });
                };
                BoxCategoryComp.prototype.editCategory = function (e, cate) {
                    e.stopPropagation();
                    this.layer.dialog({
                        title: "添加分类",
                        dialogComponent: EditCategoryComp,
                        parent: this.parent,
                        data: { category: cate, boxId: this.boxId },
                        imports: [forms_1.FormsModule]
                    });
                };
                return BoxCategoryComp;
            }());
            BoxCategoryComp = __decorate([
                core_1.Component({
                    selector: '.box_category',
                    templateUrl: 'template/router/boxProduct/category.html'
                }),
                __metadata("design:paramtypes", [http_1.Http,
                    router_1.ActivatedRoute, typeof (_a = typeof ng2_layer_1.NgLayer !== "undefined" && ng2_layer_1.NgLayer) === "function" && _a || Object, core_1.ViewContainerRef])
            ], BoxCategoryComp);
            exports_1("BoxCategoryComp", BoxCategoryComp);
            AddCategoryComp = (function () {
                function AddCategoryComp(ly, http) {
                    this.ly = ly;
                    this.http = http;
                }
                AddCategoryComp.prototype.save = function () {
                    var _this = this;
                    this.http.post({
                        search: {
                            m: "Admin",
                            c: "ApiGoodsType",
                            a: "addGoodsType"
                        },
                        body: {
                            name: this.name,
                            box_id: this.parent.boxId
                        }
                    }).then(function (data) {
                        _this.parent.categories.push(data.data);
                        _this.ly.close();
                    });
                };
                return AddCategoryComp;
            }());
            AddCategoryComp = __decorate([
                core_1.Component({
                    templateUrl: "template/router/boxProduct/addCategory.html"
                }),
                __metadata("design:paramtypes", [typeof (_b = typeof ng2_layer_1.NgLayerRef !== "undefined" && ng2_layer_1.NgLayerRef) === "function" && _b || Object, http_1.Http])
            ], AddCategoryComp);
            EditCategoryComp = (function () {
                function EditCategoryComp(ly, http) {
                    this.ly = ly;
                    this.http = http;
                }
                EditCategoryComp.prototype.ngOnInit = function () {
                    this.tempCate = Object.assign({}, this.category);
                };
                EditCategoryComp.prototype.save = function () {
                    var _this = this;
                    this.http.post({
                        search: {
                            m: "Admin",
                            c: "ApiGoodsType",
                            a: "updateType"
                        },
                        body: {
                            name: this.tempCate.name,
                            type_id: this.category.type_id
                        }
                    }).then(function (data) {
                        Object.assign(_this.category, _this.tempCate);
                        _this.ly.close();
                    });
                };
                return EditCategoryComp;
            }());
            EditCategoryComp = __decorate([
                core_1.Component({
                    templateUrl: "template/router/boxProduct/editCategory.html"
                }),
                __metadata("design:paramtypes", [typeof (_c = typeof ng2_layer_1.NgLayerRef !== "undefined" && ng2_layer_1.NgLayerRef) === "function" && _c || Object, http_1.Http])
            ], EditCategoryComp);
        }
    };
});
